// 寻找峰值

package Leetcode;

import java.util.ArrayList;
import java.util.List;

class Solution162_1 {
    public List<Integer> result = new ArrayList<>();

    public int findPeakElement(int[] nums) {
        // 如果数组中只有一个元素
        if (nums.length == 1)
            return 0;
        find(nums, 0, nums.length - 1);
        return result.get(0);
    }

    public void find(int[] nums, int left, int right) {
        // 递归停止
        if (left > right)
            return;
        int mid = (left + right) / 2;
        int leftValue = mid - 1 == -1 ? Integer.MIN_VALUE : nums[mid - 1];
        int rightValue = mid + 1 == nums.length ? Integer.MIN_VALUE : nums[mid + 1];
        if(nums[mid] > leftValue && nums[mid] > rightValue){
            result.add(mid);
        }

        else{
            // find(nums, left, mid - 1);
            // find(nums, mid + 1, right);
            if(nums[mid] > rightValue)
                find(nums, left, mid - 1);
            else
                find(nums, mid + 1, right);
        }
    }
}